package com.anysoft;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;

import com.anysoft.utils.AppInfoUtils;
import com.anysoft.utils.StringUtils;
import com.meituan.android.walle.WalleChannelReader;

public class StaticField {

    public static String token;

    /**
     * 指示是否打开过登录
     */
    public static boolean isOpenLogin;
    private static String app_name;
    private static String channel;
    private static String versionName;
    private static String packageName;
    private static Integer versionCode;

    public static String getApp_name(Context context) {
        if (app_name == null) {
            app_name = context.getResources().getString(R.string.app_name);
        }
        return app_name;
    }

    public static String getChannel(Context context) {
        if (channel == null) {
//            channel = AppInfoUtils.getAppMetaData(context, "UMENG_CHANNEL");
            channel = StringUtils.isNullOrEmpty(WalleChannelReader.getChannel(context))? AppInfoUtils.getAppMetaData
                    (context, "UMENG_CHANNEL"):WalleChannelReader.getChannel(context);
        }
        return channel;
    }

    /**
     * 版本名，如：3.5.2
     */
    public static String getVersionName(Context context) {
        if (versionName == null) {
            versionName = getPackageInfo(context).versionName;
        }
        return versionName;
    }

    /**
     * 版本代号，如：20150520
     */
    public static Integer getVersionCode(Context context) {
        if (versionCode == null) {
            versionCode = getPackageInfo(context).versionCode;
        }
        return versionCode;
    }

    /**
     * 包名
     */
    public static String getPackageName(Context context) {
        if (packageName == null) {
            packageName = getPackageInfo(context).packageName;
        }
        return packageName;
    }

    public static PackageInfo getPackageInfo(Context context) {
        try {
            return context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

}
